package com.eva.admin.api.cms;

import com.eva.admin.biz.cms.resource.CmsResourceBiz;
import com.eva.admin.biz.cms.resource.dto.CreateCmsResourceDTO;
import com.eva.admin.biz.cms.resource.dto.QueryCmsResourceDTO;
import com.eva.admin.biz.cms.resource.dto.UpdateCmsResourceDTO;
import com.eva.admin.biz.cms.resource.dto.UpdateCmsResourceStatusDTO;
import com.eva.admin.biz.cms.resource.vo.CmsResourceVO;
import com.eva.framework.cache.autowired.CleanCache;
import com.eva.framework.common.api.BaseController;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "资源管理")
@RestController
@RequestMapping("/cms/res")
public class CmsResourceController extends BaseController {

    @Resource
    private CmsResourceBiz cmsResourceBiz;

    @PreventRepeat
    @CleanCache("resource:group:*:resources")
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("cms:resource:create")
    public ApiResponse<?> create(@RequestBody CreateCmsResourceDTO dto) {
        cmsResourceBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("删除")
    @CleanCache("resource:group:*:resources")
    @GetMapping("/delete/{id}")
    @ContainPermissions("cms:resource:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        cmsResourceBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @CleanCache("resource:group:*:resources")
    @GetMapping("/delete/batch")
    @ContainPermissions("cms:resource:delete")
    public ApiResponse<?> deleteByIdInBatch(@RequestParam String ids) {
        cmsResourceBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("修改")
    @CleanCache("resource:group:*:resources")
    @PostMapping("/updateById")
    @ContainPermissions("cms:resource:update")
    public ApiResponse<?> updateById(@RequestBody UpdateCmsResourceDTO dto) {
        cmsResourceBiz.updateById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("修改状态")
    @CleanCache("resource:group:*:resources")
    @PostMapping("/updateStatus")
    @ContainPermissions("cms:resource:update")
    public ApiResponse<?> updateStatus(@RequestBody UpdateCmsResourceStatusDTO dto) {
        cmsResourceBiz.updateStatus(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("cms:resource:query")
    public ApiResponse<PageData<CmsResourceVO>> findPage (@RequestBody PageWrap<QueryCmsResourceDTO> pageWrap) {
        return ApiResponse.success(cmsResourceBiz.findPage(pageWrap));
    }
}
